package nameless.aistudy.global.config;


import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class APIDocConfig {

    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI()
                .info(new Info()
                    .title("AI辅助学习系统API文档")
                    .version("0.0.1")
                    .description("")
                );
//                .externalDocs(new ExternalDocumentation()
//                        .description("参考文档")
//                        .url("https://doc.xiaominfo.com"));
    }

    @Bean("assignTagsByPath")
    public OpenApiCustomizer assignTagsByPath() {
        return openApi -> {
            // 根据路径规则分配标签
            openApi.getPaths().forEach((path, pathItem) -> {

                // 清除所有tags
                pathItem.readOperations().forEach(op -> op.setTags(null));

                if (path.startsWith("/user/send-code")
                        || path.startsWith("/user/register")
                        || path.startsWith("/user/login")
                        || path.startsWith("/user/token")
                ) {
                    pathItem.readOperations().forEach(op -> op.addTagsItem("用户认证"));
                }

                else if (path.equals("/user")
                        || path.equals("/user/checkin")
                        || path.equals("/user/profile")
                ) {
                    pathItem.readOperations().forEach(op -> op.addTagsItem("用户基本操作"));
                }

                else if (path.startsWith("/user/statistics")
                        || path.equals("/user/subject/statistics")
                ) {
                    pathItem.readOperations().forEach(op -> op.addTagsItem("用户统计"));
                }

                else if (path.startsWith("/user/subject/english")) {
                    pathItem.readOperations().forEach(op -> op.addTagsItem("英语学科"));

                    String subsequentPath = path.replace("/user/subject/english", "");
                    if (subsequentPath.startsWith("/vocabulary")) {
                        pathItem.readOperations().forEach(op -> op.addTagsItem("英语词汇"));
                    }
                    else if (subsequentPath.startsWith("/reading")) {
                        pathItem.readOperations().forEach(op -> op.addTagsItem("英语阅读"));
                    }
                    else if (subsequentPath.startsWith("/writing")) {
                        pathItem.readOperations().forEach(op -> op.addTagsItem("英语作文"));
                    }
                    else if (subsequentPath.startsWith("/plan")) {
                        pathItem.readOperations().forEach(op -> op.addTagsItem("英语计划"));
                    }
                    else if (subsequentPath.startsWith("/statistics")) {
                        pathItem.readOperations().forEach(op -> op.addTagsItem("英语统计"));
                    }
                }
            });
        };
    }

    @Bean
    public GroupedOpenApi superAdminApi(@Qualifier("assignTagsByPath") OpenApiCustomizer assignTagsByPath) {
        return GroupedOpenApi.builder()
                .group("超管接口")
                .packagesToScan("nameless.aistudy.api.role.admin")
                .pathsToMatch("/super-admin/**")
                .addOpenApiCustomizer(assignTagsByPath)
                .build();
    }

    @Bean
    public GroupedOpenApi adminApi(@Qualifier("assignTagsByPath") OpenApiCustomizer assignTagsByPath) {
        return GroupedOpenApi.builder()
                .group("管理接口")
                .packagesToScan("nameless.aistudy.api.role.admin")
                .pathsToMatch("/admin/**")
                .addOpenApiCustomizer(assignTagsByPath)
                .build();
    }

    @Bean
    public GroupedOpenApi userApi(@Qualifier("assignTagsByPath") OpenApiCustomizer assignTagsByPath) {
        return GroupedOpenApi.builder()
                .group("用户接口")
                .packagesToScan("nameless.aistudy.api.role.user")
                .pathsToMatch("/user/**")
                .addOpenApiCustomizer(assignTagsByPath)
                .build();
    }

    @Bean
    public GroupedOpenApi authApi(@Qualifier("assignTagsByPath") OpenApiCustomizer assignTagsByPath) {
        return GroupedOpenApi.builder()
                .group("认证接口")
                .packagesToScan("nameless.aistudy.api.auth")
                .pathsToMatch("/user/**")
                .addOpenApiCustomizer(assignTagsByPath)
                .build();
    }
}
